<?php
/**
 * Created by PhpStorm.
 * User: HS032
 * Date: 2020/8/21
 * Time: 17:45
 */

namespace ticket\modules\backend\controllers;


use ticket\libs\Tools;
use ticket\modules\backend\models\TicketAddionalAttr;
use ticket\modules\backend\models\TicketInfo;
use Yii;

/*** 门票自定义字段
 * Class TicketAttrController
 * @package ticket\modules\backend\controllers
 */
class TicketAttrController extends BasicController
{

    /*** 自定义字段列表
     * @return false|string
     * @throws \yii\db\Exception
     */
    public function actionAttrInfoList()
    {
        $get = Yii::$app->request->get();

        $page = isset($get['page'])?$get['page']:1;
        $pageSize = isset($get['size'])?$get['size']:10;
        $name = $get['name'];
        $aid = $this->appId;
        if (empty($aid)){
            $aid = isset($get['app_id'])?$get['app_id']:'';
        }
        if  (isset($name) and !empty($name)){
            $where = "attr_name like '%$name%' and a.aid = $aid";
        }else{
            $where = " a.aid = $aid";
        }
        $offset = $pageSize * ($page - 1);
        $sql = "select a.tid, a.name,b.* from tk_ticket_info a left join tk_ticket_addional_attr b on a.tid = b.tid where {$where} limit {$offset}, {$pageSize}";
        $attrList = Yii::$app->db->createCommand($sql)->queryAll();

        foreach ($attrList as $key => $val) {
            if ($val['id'] == ''){
                unset($attrList[$key]);
            }
        }


        return Tools::showRes(0,'success',[
            'attrList' => $attrList,
        ]);
    }


    /*** 添加自定义数据
     * @return false|string
     */
    public function actionAddTicketAttr()
    {
        if (Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
            $attrModel = new TicketAddionalAttr();
            if (!$attrModel->addAttr($post)){
                if ($attrModel->hasErrors()){
                    return Tools::showRes(99,$attrModel->getErrors());
                }else{
                    return Tools::showRes(99,'添加失败');
                }
            }

            return Tools::showRes(0,'添加成功');
        }
    }


    /*** 修改自定义数据
     * @return false|string
     */
    public function actionModTicketAttr()
    {
        if (Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
            $attrModel = new TicketAddionalAttr();
            $id = isset($post['id'])?$post['id']:'';
            if (!$attrModel->modAttr($id,$post)){
                if ($attrModel->hasErrors()){
                    return Tools::showRes(99,$attrModel->getErrors());
                }else{
                    return Tools::showRes(99,'修改失败');
                }
            }

            return Tools::showRes(0,'修改成功');

        }
    }

    /*** 获取门票列表
     * @return false|string
     */
    public function actionGetTicketList()
    {
        $app_id = $this->appId;

        $ticketModel = new TicketInfo();
        $ticketList = $ticketModel->getTicketList($app_id);

        if (empty($ticketList)){
            $ticketList = [];
        }
        return Tools::showRes(0,'success',[
            'ticketList' => $ticketList,
        ]);
    }

    /*** 获取自定义数据信息
     * @return false|string
     */
    public function actionGetTicketAttr()
    {
        $get = Yii::$app->request->get();
        $aid = $this->appId;
        $id = isset($get['id'])?$get['id']:'';
        if  (empty($id)){
            return Tools::showRes(99,'参数有误');
        }
        $ticketModel = new TicketInfo();
        $attrModel = new TicketAddionalAttr();
        $attrInfo = $attrModel->getAttrInfo($id);
        $ticketList = $ticketModel->getTicketList($aid);
        return Tools::showRes(0,'success',[
            'attrInfo' => $attrInfo,
            'ticketList' => $ticketList,
        ]);
    }



    /*** 删除信息
     * @return false|string
     * @throws \Throwable
     * @throws \yii\db\StaleObjectException
     */
    public function actionDelTicketAttr()
    {
        if (Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
            $id = isset($post['id'])?$post['id']:'';
            if  (empty($id)){
                return Tools::showRes(99,'参数有误');
            }
            $attrModel = new TicketAddionalAttr();
            if ($attrModel->delTicketAttr($id)){
                return Tools::showRes(0,'删除成功');
            }
            return Tools::showRes(99,'删除失败');
        }
    }


    
}